<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}在线跑批系统{% endblock %}</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <!-- 自定义CSS -->
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .content {
            flex: 1;
        }
        .sidebar {
            min-height: calc(100vh - 56px);
            background-color: #f8f9fa;
            border-right: 1px solid #dee2e6;
        }
        .sidebar .nav-link {
            color: #333;
            padding: 0.75rem 1rem;
        }
        .sidebar .nav-link.active {
            background-color: #0d6efd;
            color: white;
        }
        .sidebar .nav-link:hover:not(.active) {
            background-color: #e9ecef;
        }
        .sidebar .nav-item .nav-link i {
            width: 20px;
            margin-right: 8px;
        }
        .sidebar .nav-item .nav-link .bi-chevron-down {
            margin-left: auto;
        }
        .sidebar .submenu {
            padding-left: 1.5rem;
        }
        .sidebar .submenu .nav-link {
            padding: 0.5rem 1rem;
        }
        .card {
            transition: transform 0.2s, box-shadow 0.2s;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .table-responsive {
            overflow-x: auto;
        }
        .footer {
            background-color: #f8f9fa;
            padding: 1rem 0;
            border-top: 1px solid #dee2e6;
        }
    </style>
    {% block extra_css %}{% endblock %}
</head>
<body><form method="POST" id="form1"> 
    <!-- 导航栏 -->
    <!-- <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{% url 'dashboard' %}">
                <i class="fas fa-cogs me-2"></i>在线跑批系统2
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown">
                            <i class="fas fa-user me-1"></i>{{ user.username }}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="{% url 'dashboard' %}"><i class="fas fa-tachometer-alt me-2"></i>仪表盘</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="{% url 'logout' %}"><i class="fas fa-sign-out-alt me-2"></i>退出登录</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav> -->

    <div class="container-fluid content">
        <div class="row">
            <!-- 侧边栏 -->
            <!-- <div class="col-md-2 d-none d-md-block sidebar p-0">
                <div class="list-group list-group-flush">
                    <a href="{% url 'dashboard' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'dashboard' %}active{% endif %}">
                        <i class="fas fa-tachometer-alt"></i>仪表盘
                    </a>
                    
                    <div class="list-group-item p-0">
                        <a href="#serverSubmenu" class="list-group-item list-group-item-action w-100 d-flex justify-content-between align-items-center" data-bs-toggle="collapse">
                            <span><i class="fas fa-server"></i>服务器管理</span>
                            <i class="bi bi-chevron-down"></i>
                        </a>
                        <div class="collapse {% if 'server' in request.resolver_match.url_name %}show{% endif %}" id="serverSubmenu">
                            <a href="{% url 'server_list' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'server_list' %}active{% endif %}">
                                <i class="fas fa-list"></i>服务器列表
                            </a>
                            <a href="{% url 'server_add' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'server_add' %}active{% endif %}">
                                <i class="fas fa-plus"></i>添加服务器
                            </a>
                        </div>
                    </div>
                    
                    <div class="list-group-item p-0">
                        <a href="#taskSubmenu" class="list-group-item list-group-item-action w-100 d-flex justify-content-between align-items-center" data-bs-toggle="collapse">
                            <span><i class="fas fa-tasks"></i>批处理任务</span>
                            <i class="bi bi-chevron-down"></i>
                        </a>
                        <div class="collapse {% if 'task' in request.resolver_match.url_name %}show{% endif %}" id="taskSubmenu">
                            <a href="{% url 'task_list' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'task_list' %}active{% endif %}">
                                <i class="fas fa-list"></i>任务列表
                            </a>
                            <a href="{% url 'task_add' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'task_add' %}active{% endif %}">
                                <i class="fas fa-plus"></i>创建任务
                            </a>
                        </div>
                    </div>
                    
                    <a href="{% url 'log_list' %}" class="list-group-item list-group-item-action {% if 'log' in request.resolver_match.url_name %}active{% endif %}">
                        <i class="fas fa-file-alt"></i>日志管理
                    </a>
                    
                    <div class="list-group-item p-0">
                        <a href="#reportSubmenu" class="list-group-item list-group-item-action w-100 d-flex justify-content-between align-items-center" data-bs-toggle="collapse">
                            <span><i class="fas fa-chart-pie"></i>报表统计</span>
                            <i class="bi bi-chevron-down"></i>
                        </a>
                        <div class="collapse {% if 'report' in request.resolver_match.url_name %}show{% endif %}" id="reportSubmenu">
                            <a href="{% url 'task_summary_report' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'task_summary_report' %}active{% endif %}">
                                <i class="fas fa-tasks"></i>任务汇总报表
                            </a>
                            <a href="{% url 'server_activity_report' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'server_activity_report' %}active{% endif %}">
                                <i class="fas fa-server"></i>服务器活动报表
                            </a>
                        </div>
                    </div>
              
                    <div class="list-group-item p-0">
                        <a href="#dbSubmenu" class="list-group-item list-group-item-action w-100 d-flex justify-content-between align-items-center" data-bs-toggle="collapse">
                            <span><i class="fas fa-database"></i>数据库管理</span>
                            <i class="bi bi-chevron-down"></i>
                        </a>
                        <div class="collapse {% if 'db-connection' in request.resolver_match.url_name or 'db-tools' in request.resolver_match.url_name %}show{% endif %}" id="dbSubmenu">
                            <a href="{% url 'db_connection_list' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'db_connection_list' %}active{% endif %}">
                                <i class="fas fa-link"></i>连接配置
                            </a>
                            <a href="{% url 'sql_query_test' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'sql_query_test' %}active{% endif %}">
                                <i class="fas fa-search"></i>查询测试
                            </a>
                            <a href="{% url 'stored_procedure_analysis' %}" class="list-group-item list-group-item-action {% if request.resolver_match.url_name == 'stored_procedure_analysis' %}active{% endif %}">
                                <i class="fas fa-code"></i>存储过程分析
                            </a>
                        </div>
                    </div>
                    <a href="/admin/" class="list-group-item list-group-item-action">
                        <i class="fas fa-tools"></i>系统管理
                    </a>
                </div>
            </div> -->

            <!-- 主内容区 -->
            <main class="col-md-12 ms-sm-auto px-md-4 py-4">
                {% block content %}{% endblock %}
            </main>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="footer mt-auto">
        <div class="container-fluid">
            <div class="text-center text-muted">
                <p class="mb-0">&copy; {% now "Y" %} 在线跑批系统 - 版权所有</p>
            </div>
        </div>
    </footer>

    <!-- Bootstrap JS Bundle with Popper -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/js/bootstrap.bundle.min.js"></script>
    <!-- Plotly JS -->
    <script src="http://cdn.plot.ly/plotly-latest.min.js"></script>
    <!-- 自定义JS -->
    <script>
        // 处理服务器连接测试
        document.addEventListener('DOMContentLoaded', function() {
            // 测试连接按钮点击事件
            const testButtons = document.querySelectorAll('.test-connection-btn');
            testButtons.forEach(button => {
                button.addEventListener('click', function(e) {
                    e.preventDefault();
                    const serverId = this.getAttribute('data-server-id');
                    const url = `/servers/test-connection/${serverId}/`;
                    const btnText = this.innerHTML;
                    
                    // 禁用按钮并显示加载状态
                    this.disabled = true;
                    this.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 测试中...';
                    
                    // 发送请求
                    fetch(url)
                        .then(response => response.json())
                        .then(data => {
                            // 恢复按钮状态
                            this.disabled = false;
                            this.innerHTML = btnText;
                            
                            // 显示结果
                            const alertClass = data.success ? 'alert-success' : 'alert-danger';
                            const alertDiv = document.createElement('div');
                            alertDiv.className = `alert ${alertClass} alert-dismissible fade show mt-3`;
                            alertDiv.role = 'alert';
                            alertDiv.innerHTML = `
                                ${data.message}
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            `;
                            
                            // 添加到页面
                            this.parentNode.appendChild(alertDiv);
                            
                            // 3秒后自动关闭
                            setTimeout(() => {
                                const bsAlert = new bootstrap.Alert(alertDiv);
                                bsAlert.close();
                            }, 3000);
                        })
                        .catch(error => {
                            // 错误处理
                            this.disabled = false;
                            this.innerHTML = btnText;
                            
                            const alertDiv = document.createElement('div');
                            alertDiv.className = 'alert alert-danger alert-dismissible fade show mt-3';
                            alertDiv.role = 'alert';
                            alertDiv.innerHTML = `
                                测试连接时发生错误
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                            `;
                            
                            this.parentNode.appendChild(alertDiv);
                        });
                });
            });
            
            // 任务执行按钮点击事件
            const runButtons = document.querySelectorAll('.run-task-btn');
            runButtons.forEach(button => {
                button.addEventListener('click', function(e) {
                    if (!confirm('确定要立即执行此任务吗？')) {
                        e.preventDefault();
                    }
                });
            });
            
            // 删除按钮确认
            const deleteButtons = document.querySelectorAll('.delete-btn');
            deleteButtons.forEach(button => {
                button.addEventListener('click', function(e) {
                    const itemName = this.getAttribute('data-item-name');
                    if (!confirm(`确定要删除 ${itemName} 吗？此操作不可恢复。`)) {
                        e.preventDefault();
                    }
                });
            });
        });
    </script>
    {% block extra_js %}{% endblock %}
    </form>
</body>
</html>
